/*
  S.M.A.C.K - An operating system kernel
  Copyright (C) 2010,2011 Mattias Holm and Kristian Rietveld
  For licensing and a full list of authors of the kernel, see the files
  COPYING and AUTHORS.
*/

#include <stdio.h>
#include <syscalls.h>

#define N_PROC 4

static void
run_finite_child(int n)
{
  uint32_t foo = 3;
  int i;

  for (i = 0; i < 1000000000 / 2; i++)
    foo *= 3;

  puts ("finished");
  exit(0);
}

static void
run_indefinite_child(int n)
{
  /* For now all processes will busy loop, showing equalized load */
  while(1);

  exit(0);
}

static void
daemon_main(void)
{
  int j;
  int i;
  int pid;

  for (j = 0; j < 5; j++)
    {
      printf("Will spawn %d processes.\n", N_PROC);

      for (i = 0; i < N_PROC; i++)
        {
          pid = fork();

          if (pid == 0)
            run_finite_child(i);

          printf("%d ", pid);
        }

      printf (" OK\n");

      sleep (10);
    }

  /* Hang - wait on processes to exit */
  for (i = 0; i < N_PROC; i++)
    wait(NULL);

  exit(0);
}

int
main(int argc, char **argv)
{
  int pid;

  /* deamonize */
  pid = fork();

  if (pid == 0)
    daemon_main();

  return 0;
}
